package com.ruoyi.zsyh.controller.applet.after.factory;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.ZsyhAfterProduct;
import com.ruoyi.zsyh.domain.ZsyhAfterProductType;
import com.ruoyi.zsyh.domain.ZsyhServiceOrder;
import com.ruoyi.zsyh.service.IZsyhAfterProductService;
import com.ruoyi.zsyh.service.IZsyhAfterProductTypeService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 服务订单 -- 厂家售后产品管理
 */
@Controller
@RequestMapping("/applet/zsyh/afterProduct")
public class ZsyhAfterProductAppletController extends BaseController
{

    @Autowired
    private IZsyhAfterProductService zsyhAfterProductService;

    @Autowired
    private IZsyhAfterProductTypeService zsyhAfterProductTypeService;

    /**
     * 厂家 常用产品型号
     * @return
     */
    @RequestMapping("/getProductList")
    @ResponseBody
    public AjaxResult getProductList(String factoryId, String key)throws Exception
    {
        List<ZsyhAfterProduct> list = zsyhAfterProductService.getZsyhAfterProductByFactoryId(factoryId, key);
        return success("成功", list);
    }

    /**
     * 厂家 获取产品类型列表
     * @return
     */
    @RequestMapping("/getProductTypeList")
    @ResponseBody
    public AjaxResult getProductTypeList(String factoryId)throws Exception
    {
        ZsyhAfterProductType type = new ZsyhAfterProductType();
        List<ZsyhAfterProductType> list = zsyhAfterProductTypeService.selectZsyhAfterProductTypeList(type);
        return success("成功", list);
    }

    /**
     * 厂家 获取产品规格列表
     * @return
     */
    @RequestMapping("/getProductListByTypeId")
    @ResponseBody
    public AjaxResult getProductListByTypeId(String factoryId, String typeId)throws Exception
    {
        ZsyhAfterProduct product = new ZsyhAfterProduct();
        product.setFactoryId(factoryId);
        product.setProductType(typeId);
        product.setProductState("1");
        List<ZsyhAfterProduct> list = zsyhAfterProductService.selectZsyhAfterProductList(product);
        return success("成功", list);
    }

    /**
     * 厂家 获取产品规格列表
     * @return
     */
    @RequestMapping("/getProductListByFactoryId")
    @ResponseBody
    public AjaxResult getProductListByFactoryId(String pageNum, String pageSize, String factoryId, String typeId,
        String productBrand, String productModel, String productAttribute, String productState)throws Exception
    {
        ZsyhAfterProduct product = new ZsyhAfterProduct();
        product.setFactoryId(factoryId);
        product.setProductType(typeId);
        product.setProductBrand(productBrand);
        product.setProductModel(productModel);
        product.setProductAttribute(productAttribute);
        product.setProductState(productState);
        List<ZsyhAfterProduct> listSize = zsyhAfterProductService.selectZsyhAfterProductList(product);
        int total = listSize.size();
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        product.setParams(map);
        List<ZsyhAfterProduct> list = zsyhAfterProductService.selectZsyhAfterProductList(product);
        return success("成功", list, total);
    }

    /**
     * 保存产品信息
     * @param factoryId
     * @param typeId
     * @param productAttribute
     * @param productBrand
     * @param productModel
     * @param productImages
     * @param productFault
     * @param productRemarks
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveProduct")
    @ResponseBody
    public AjaxResult saveProduct(String factoryId, String typeId, String productAttribute, String productBrand, String productModel,
            String productImages, String productFault, String productRemarks)throws Exception
    {
        ZsyhAfterProduct product = new ZsyhAfterProduct();
        product.setId(IdWorker.getId());
        product.setFactoryId(factoryId);
        product.setProductType(typeId);
        product.setProductAttribute(productAttribute);
        product.setProductBrand(productBrand);
        product.setProductModel(productModel);
        product.setProductImages(productImages);
        product.setProductFault(productFault);
        product.setProductRemarks(productRemarks);
        product.setProductState("1");
        zsyhAfterProductService.insertZsyhAfterProduct(product);
        return success("成功");
    }

    /**
     * 修改我的产品
     * @param id
     * @param typeId
     * @param productAttribute
     * @param productBrand
     * @param productModel
     * @param productImages
     * @param productFault
     * @param productRemarks
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateProduct")
    @ResponseBody
    public AjaxResult updateProduct(String id, String typeId, String productAttribute, String productBrand, String productModel,
                                  String productImages, String productFault, String productRemarks)throws Exception
    {
        ZsyhAfterProduct product = new ZsyhAfterProduct();
        product.setId(id);
        product.setProductType(typeId);
        product.setProductAttribute(productAttribute);
        product.setProductBrand(productBrand);
        product.setProductModel(productModel);
        product.setProductImages(productImages);
        product.setProductFault(productFault);
        product.setProductRemarks(productRemarks);
        zsyhAfterProductService.updateZsyhAfterProduct(product);
        return success("成功");
    }

    /**
     * 商品移入移除回收站
     * @param id
     * @param productState
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateProductState")
    @ResponseBody
    public AjaxResult updateProductState(String id, String productState)throws Exception
    {
        ZsyhAfterProduct product = new ZsyhAfterProduct();
        product.setId(id);
        product.setProductState(productState);
        zsyhAfterProductService.updateZsyhAfterProduct(product);
        return success("成功");
    }

    /**
     * 回收站商品删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delProduct")
    @ResponseBody
    public AjaxResult delProduct(String id)throws Exception
    {
        zsyhAfterProductService.deleteZsyhAfterProductById(id);
        return success("成功");
    }
}
